package com.breaking.second.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.breaking.second.R;
import com.breaking.second.activity.pager.adapter.BuyGoodsAdapter;
import com.breaking.second.databinding.ActivitySellGoodsBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.BuyGoodsEntity;
import com.breaking.second.entity.PageableEntity;
import com.breaking.second.utils.compat.FormBodyCompat;
import com.breaking.second.utils.compat.RefreshLayoutCompat;
import com.breaking.second.utils.compat.SkeltonCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.ethanhua.skeleton.ViewSkeletonScreen;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

//已卖出商品列表
public class SellGoodsActivity extends BaseActivity<ActivitySellGoodsBinding> {
    private final List<BuyGoodsEntity> goodsEntities = new ArrayList<>();
    private BuyGoodsAdapter buyGoodsAdapter;
    private ViewSkeletonScreen d;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initRecycler();
        initSmart();
        initSkelton();
    }


    private void initSkelton() {
        d = SkeltonCompat.bind(binding.smart, R.layout.layout_empty).show();
    }

    private void initSmart() {
        RefreshLayoutCompat.setRefresh(pageableVo, binding.smart, new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
    }

    private void initRecycler() {
        buyGoodsAdapter = new BuyGoodsAdapter(this, goodsEntities);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(buyGoodsAdapter);
    }

    private void initData() {
        getData();
    }

    private void getData() {
        FormBody.Builder form = FormBodyCompat.to(pageableVo);
        OkhttpUtil.Get("trading/sell", form, new OkhttpUtil.CallbackListener(this::getData), binding.smart);
    }

    private void getData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            PageableEntity pageable = bookEntity.to(PageableEntity.class);
            if (pageable == null) return;
            List<BuyGoodsEntity> buyGoodsEntities = pageable.toList(BuyGoodsEntity.class);
            if (buyGoodsEntities == null) return;
            if (pageable.getCurrent() == 0) {
                buyGoodsAdapter.notifyItemRangeRemoved(0, this.goodsEntities.size());
                this.goodsEntities.clear();
            }
            int start = this.goodsEntities.size();
            this.goodsEntities.addAll(buyGoodsEntities);
            buyGoodsAdapter.notifyItemRangeChanged(start, this.goodsEntities.size());

            if (!this.goodsEntities.isEmpty()) {
                d.hide();
            }
        }
    }
}
